//
//  PictureViewCell.m
//  Product_NEWS
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 FCH. All rights reserved.
//

#import "PictureViewCell.h"
#import "UIImageView+WebCache.h"
@interface PictureViewCell ()
@property (nonatomic, retain) titleLabel *titleLabel; // 内容
@property (nonatomic, retain) UIImageView *profileImageView; // 用户头像
@property (nonatomic, retain) UILabel *userNameLabel; // 用户名
@property (nonatomic, retain) UIButton *favoritesButton;
@property (nonatomic, retain) WordTite *timeLabel; // 发布时间
@end


@implementation PictureViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
         [[SDImageCache sharedImageCache] clearMemory];
    }
    return self;
}


-(void)createSubViews
{
//   self.contentView.backgroundColor = [UIColor colorWithWhite:0.929 alpha:1.000];
    // 用户头像
    self.profileImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * SCREEN_WIDTH, 10 * SCREEN_HEIGHT, 50 * SCREEN_WIDTH, 50 * SCREEN_HEIGHT)];
    // 设置图片圆角
    // 1.设置图片边框为隐藏状态
    self.profileImageView.layer.masksToBounds = YES;
    // 2.设置图片圆角
    self.profileImageView.layer.cornerRadius = 25; // 为imageView大小的一半即为圆
    [self.contentView addSubview:self.profileImageView];
    
    // 用户名
    self.userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80 * SCREEN_WIDTH, 10 * SCREEN_HEIGHT, 200 * SCREEN_WIDTH, 30 * SCREEN_HEIGHT)];
    self.userNameLabel.font = [UIFont systemFontOfSize:14*SCREEN_WIDTH];
    self.userNameLabel.textColor = [NightModeHandle shareNightModeHandle].titleColor;
    self.userNameLabel.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    
    
    [self addSubview:self.userNameLabel];
    
    self.timeLabel = [[WordTite alloc] initWithFrame:CGRectMake(80 * SCREEN_WIDTH, 40 * SCREEN_HEIGHT, 200 * SCREEN_WIDTH, 20 * SCREEN_HEIGHT)];
    self.timeLabel.font = [UIFont systemFontOfSize:12*SCREEN_WIDTH];
   
    [self addSubview:self.timeLabel];
    self.titleLabel = [[titleLabel alloc] initWithFrame:CGRectMake(10* SCREEN_WIDTH, 70* SCREEN_HEIGHT, 300* SCREEN_WIDTH, 30* SCREEN_HEIGHT)];
    
    self.titleLabel.font = [UIFont systemFontOfSize:16*SCREEN_WIDTH];

    
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [self.contentView addSubview:self.titleLabel];
    self.picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100 * SCREEN_HEIGHT, 355 * SCREEN_WIDTH, 100 * SCREEN_HEIGHT)];
    
    self.picImageView.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    [self.contentView addSubview:self.picImageView];
    

    
}

-(void)setModel:(PictureModel *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    
    CGFloat picWidth = [[UIScreen mainScreen] bounds].size.width - 20; // 保证宽度一致
    CGFloat scale = picWidth / _model.width; // 缩放比例
    CGFloat picHeight = (CGFloat)_model.height * scale; // 保证比例不变
    if (picHeight > 667.0) {
        picHeight = 300;
        self.picImageView.contentMode = UIViewContentModeScaleAspectFit;
    }
    self.picImageView.frame = CGRectMake(0, 100 * SCREEN_HEIGHT, 355 *SCREEN_WIDTH , picHeight);
   
    [self.profileImageView sd_setImageWithURL:[NSURL URLWithString:model.profile_image] placeholderImage:[UIImage imageNamed:@"background"]];
    
    self.userNameLabel.text = model.screen_name;
    
    self.timeLabel.text = model.created_at;
    self.titleLabel.text = model.text;
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:model.image1] placeholderImage:[UIImage imageNamed:@"background"]];

}






//自适应高度
+ (CGFloat)heightForText:(NSString *)text width:(CGFloat)width{
    
    CGSize size = CGSizeMake(width, 3000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16.0*SCREEN_WIDTH] forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;

}



@end
